package com.ctrip.demo.leetcode;

public class LeetCode6 {


    public String convert(String s, int numRows) {


        int n = s.length();
        if (numRows == 1 || numRows >= n) {
            return s;
        }

        StringBuffer[] stringBuffer = new StringBuffer[numRows];

        for (int i = 0; i < numRows; i++) {
            stringBuffer[i] = new StringBuffer();
        }

        int index = 0;

        int row = 0;

        while (index < n) {
            while (index < n && row < numRows) {
                char ch = s.charAt(index++);
                stringBuffer[row].append(ch);
                row ++;
            }

            if (index == n) {
                break;
            }
            row = numRows - 2;

            while (index < n && row >= 0) {
                char ch = s.charAt(index++);
                stringBuffer[row].append(ch);
                row --;
            }

            row +=2;
        }

        StringBuilder res = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            res.append(stringBuffer[i]);
        }

        return res.toString();



    }
}
